cd "$data"

*pull baseline variables 
*count charteryears


*retain only those in appropriate sample for faster processing
foreach y in  02 03 04 05 06 07 08 09 10 11 12 13 14 15  16 17 18 19 20 {
	use "$data\lottery_applicants_voting.dta", clear
	duplicates drop sasid, force
	merge 1:m sasid using "$saves/end`y'.dta", keep(3) nogen
	save "$saves/end`y'_voter.dta", replace
}
#delimit;
local files end02  end03  end04  end05  end06  end07  end08  end09  end10  end11  end12 end13 end14  end15 end16 end17 end18 end19 end20 ;
foreach f of local files {;
	use "$saves/`f'_voter.dta" , clear;
	codebook sasid;
	drop *name rfe ;
	cap drop posthsplan; 
	format sasid %12.0f;
	duplicates tag sasid school, gen(dup);
	/*when there are dups, there are always only one case with all info. (only an issue in 2001) keep that case.*/;
	drop if race==.& dup>=1;
	*a few left, keep max attended WITHIN school;
	bys sasid school: egen maxattend=max(attend);
	gen tag=1 if attend==maxattend;
	*Make sure no SASID is dropped accidentally;
	keep if dup==0|tag==1;
	duplicates drop;
	duplicates drop sasid school, force;
	drop maxattend;
	drop dup tag;
	compress;
	sort sasid school;
	codebook sasid; 
	save "$saves/`f'_votern.dta", replace;
	};
#delimit;
use "$saves\end02_votern.dta", clear;
append using "$saves\end03_votern";
append using "$saves\end04_votern";
append using "$saves\end05_votern";
append using "$saves\end06_votern";
append using "$saves\end07_votern";
append using "$saves\end08_votern";
append using "$saves\end09_votern";
append using "$saves\end10_votern";
append using "$saves\end11_votern";
append using "$saves\end12_votern";
append using "$saves\end13_votern";
append using "$saves\end14_votern";
append using "$saves\end15_votern";
append using "$saves\end16_votern";
append using "$saves\end17_votern";
append using "$saves\end18_votern";
append using "$saves\end19_votern";
append using "$saves\end20_votern";

compress;
#delimit;
sort sasid year ;
format sasid %13.0f;
	gen blah = school if year==2002|year==2003;
	tostring blah, replace;
	*keep last 3 digits;
	qui replace blah=substr(blah,-3,.);
	destring blah, replace;
	qui replace blah= org_code*10000+blah;
	qui replace school=blah if year==2002|year==2003;
	rename school masscode;
	drop blah; 
	ren org_code district;
	*drop time;
duplicates drop;

**deal with race variables;
label def race 1 "Other Race" 2 "Asian" 3 "Black" 5 "White" 99 "Hispanic" ;
gen raceFIX=.;
*asian;
replace raceFIX=2 if race63==3;
*black;
replace raceFIX=3 if race63==2;
*white;
replace raceFIX=5 if race63==1;
*native american;
replace raceFIX=4 if race63==4;
*otherrace;
*replace raceFIX=1 if (race63<=31&race63>=6)|(race63<=31&race63>=63);
replace raceFIX=1 if (race63<=63&race63>=5);
*hispanic (write over other categories);
replace raceFIX=99 if race63==33|race63==34|race63==35|race63==36|race63==37;

replace race=raceFIX if year>=2006;
drop race63;
drop raceFIX;

save "$saves/prelabel.dta", replace;

use "$saves/prelabel.dta", clear;

preserve;

use "$raw\School\schoollab13.dta", clear; 
levelsof value, local(values);
cap label drop school;
foreach v in `values' {;
	levelsof label if value==`v', local(lab);
	label define school `v' `lab', add;
};
label values value school;
save "$saves\schoollab13_c.dta", replace;
restore;

rename masscode value;
cap drop _merge;
merge m:1 value using "$saves\schoollab13_c.dta", nogen keep(1 3);
label values value school;
rename value masscode;
drop label;

label define city 1 "Abington"; label define city  2 "Acton", add; label define city 3 "Acushnet", add; label define city 4
 "Adams", add; label define city 5 "Agawam", add; label define city 6 "Alford", add; label define city 7 "Amesbury", add; label
 define city 8 "Amherst", add; label define city 9 "Andover", add; label define city 10 "Arlington", add; label define city 11
 "Ashburnham", add; label define city 12 "Ashby", add; label define city 13 "Ashfield", add; label define city 14 "Ashland", add;
 label define city 15 "Athol", add; label define city 16 "Attleboro", add; label define city 17 "Auburn", add; label define city
 18 "Avon", add; label define city 19 "Ayer", add; label define city 20 "Barnstable", add; label define city 21 "Barre", add;
 label define city 22 "Becket", add; label define city 23 "Bedford", add; label define city 24 "Belchertown", add; label define
 city 25 "Bellingham", add; label define city 26 "Belmont", add; label define city 27 "Berkley", add; label define city 28
 "Berlin", add; label define city 29 "Bernardston", add; label define city 30 "Beverly", add; label define city 31 "Billerica",
 add; label define city 32 "Blackstone", add; label define city 33 "Blandford", add; label define city 34 "Bolton", add; label
 define city 35 "Boston", add; label define city 36 "Bourne", add; label define city 37 "Boxborough", add; label define city 38
 "Boxford", add; label define city 39 "Boylston", add; label define city 40 "Braintree", add; label define city 41 "Brewster",
 add; label define city 42 "Bridgewater", add; label define city 43 "Brimfield", add; label define city 44 "Brockton", add; label
 define city 45 "Brookfield", add; label define city 46 "Brookline", add; label define city 47 "Buckland", add; label define city
 48 "Burlington", add; label define city 49 "Cambridge", add; label define city 50 "Canton", add; label define city 51
 "Carlisle", add; label define city 52 "Carver", add; label define city 53 "Charlemont", add; label define city 54 "Charlton",
 add; label define city 55 "Chatham", add; label define city 56 "Chelmsford", add; label define city 57 "Chelsea", add; label
 define city 58 "Cheshire", add; label define city 59 "Chester", add; label define city 60 "Chesterfield", add; label define city
 61 "Chicopee", add; label define city 62 "Chilmark", add; label define city 63 "Clarksburg", add; label define city 64
 "Clinton", add; label define city 65 "Cohasset", add; label define city 66 "Colrain", add; label define city 67 "Concord", add;
 label define city 68 "Conway", add; label define city 69 "Cummington", add; label define city 70 "Dalton", add; label define
 city 71 "Danvers", add; label define city 72 "Dartmouth", add; label define city 73 "Dedham", add; label define city 74
 "Deerfield", add; label define city 75 "Dennis", add; label define city 76 "Dighton", add; label define city 77 "Douglas", add;
 label define city 78 "Dover", add; label define city 79 "Dracut", add; label define city 80 "Dudley", add; label define city 81
 "Dunstable", add; label define city 82 "Duxbury", add; label define city 83 "East Bridgewater", add; label define city 84 "East
 Brookfield", add; label define city 85 "Eastham", add; label define city 86 "Easthampton", add; label define city 87 "East
 Longmeadow", add; label define city 88 "Easton", add; label define city 89 "Edgartown", add; label define city 90 "Egremont",
 add; label define city 91 "Erving", add; label define city 92 "Essex", add; label define city 93 "Everett", add; label define
 city 94 "Fairhaven", add; label define city 95 "Fall River", add; label define city 96 "Falmouth", add; label define city 97
 "Fitchburg", add; label define city 98 "Florida", add; label define city 99 "Foxborough", add; label define city 100
 "Framingham", add; label define city 101 "Franklin", add; label define city 102 "Freetown", add; label define city 103
 "Gardner", add; label define city 104 "Aquinnah", add; label define city 105 "Georgetown", add; label define city 106 "Gill",
 add; label define city 107 "Gloucester", add; label define city 108 "Goshen", add; label define city 109 "Gosnold", add; label
 define city 110 "Grafton", add; label define city 111 "Granby", add; label define city 112 "Granville", add; label define city
 113 "Great Barrington", add; label define city 114 "Greenfield", add; label define city 115 "Groton", add; label define city 116
 "Groveland", add; label define city 117 "Hadley", add; label define city 118 "Halifax", add; label define city 119 "Hamilton",
 add; label define city 120 "Hampden", add; label define city 121 "Hancock", add; label define city 122 "Hanover", add; label
 define city 123 "Hanson", add; label define city 124 "Hardwick", add; label define city 125 "Harvard", add; label define city
 126 "Harwich", add; label define city 127 "Hatfield", add; label define city 128 "Haverhill", add; label define city 129
 "Hawley", add; label define city 130 "Heath", add; label define city 131 "Hingham", add; label define city 132 "Hinsdale", add;
 label define city 133 "Holbrook", add; label define city 134 "Holden", add; label define city 135 "Holland", add; label define
 city 136 "Holliston", add; label define city 137 "Holyoke", add; label define city 138 "Hopedale", add; label define city 139
 "Hopkinton", add; label define city 140 "Hubbardston", add; label define city 141 "Hudson", add; label define city 142 "Hull",
 add; label define city 143 "Huntington", add; label define city 144 "Ipswich", add; label define city 145 "Kingston", add; label
 define city 146 "Lakeville", add; label define city 147 "Lancaster", add; label define city 148 "Lanesborough", add; label
 define city 149 "Lawrence", add; label define city 150 "Lee", add; label define city 151 "Leicester", add; label define city 152
 "Lenox", add; label define city 153 "Leominster", add; label define city 154 "Leverett", add; label define city 155 "Lexington",
 add; label define city 156 "Leyden", add; label define city 157 "Lincoln", add; label define city 158 "Littleton", add; label
 define city 159 "Longmeadow", add; label define city 160 "Lowell", add; label define city 161 "Ludlow", add; label define city
 162 "Lunenburg", add; label define city 163 "Lynn", add; label define city 164 "Lynnfield", add; label define city 165 "Malden",
 add; label define city 166 "Manchester", add; label define city 167 "Mansfield", add; label define city 168 "Marblehead", add;
 label define city 169 "Marion", add; label define city 170 "Marlborough", add; label define city 171 "Marshfield", add; label
 define city 172 "Mashpee", add; label define city 173 "Mattapoisett", add; label define city 174 "Maynard", add; label define
 city 175 "Medfield", add; label define city 176 "Medford", add; label define city 177 "Medway", add; label define city 178
 "Melrose", add; label define city 179 "Mendon", add; label define city 180 "Merrimac", add; label define city 181 "Methuen",
 add; label define city 182 "Middleborough", add; label define city 183 "Middlefield", add; label define city 184 "Middleton",
 add; label define city 185 "Milford", add; label define city 186 "Millbury", add; label define city 187 "Millis", add; label
 define city 188 "Millville", add; label define city 189 "Milton", add; label define city 190 "Monroe", add; label define city
 191 "Monson", add; label define city 192 "Montague", add; label define city 193 "Monterey", add; label define city 194
 "Montgomery", add; label define city 195 "Mount Washington", add; label define city 196 "Nahant", add; label define city 197
 "Nantucket", add; label define city 198 "Natick", add; label define city 199 "Needham", add; label define city 200 "New
 Ashford", add; label define city 201 "New Bedford", add; label define city 202 "New Braintree", add; label define city 203
 "Newbury", add; label define city 204 "Newburyport", add; label define city 205 "New Marlborough", add; label define city 206
 "New Salem", add; label define city 207 "Newton", add; label define city 208 "Norfolk", add; label define city 209 "North
 Adams", add; label define city 210 "Northampton", add; label define city 211 "North Andover", add; label define city 212 "North
 Attleborough", add; label define city 213 "Northborough", add; label define city 214 "Northbridge", add; label define city 215
 "North Brookfield", add; label define city 216 "Northfield", add; label define city 217 "North Reading", add; label define city
 218 "Norton", add; label define city 219 "Norwell", add; label define city 220 "Norwood", add; label define city 221
 "Oak Bluffs", add; label define city 222 "Oakham", add; label define city 223 "Orange", add; label define city 224 "Orleans",
 add; label define city 225 "Otis", add; label define city 226 "Oxford", add; label define city 227 "Palmer", add; label define
 city 228 "Paxton", add; label define city 229 "Peabody", add; label define city 230 "Pelham", add; label define city 231
 "Pembroke", add; label define city 232 "Pepperell", add; label define city 233 "Peru", add; label define city 234 "Petersham",
 add; label define city 235 "Phillipston", add; label define city 236 "Pittsfield", add; label define city 237 "Plainfield", add;
 label define city 238 "Plainville", add; label define city 239 "Plymouth", add; label define city 240 "Plympton", add; label
 define city 241 "Princeton", add; label define city 242 "Provincetown", add; label define city 243 "Quincy", add; label define
 city 244 "Randolph", add; label define city 245 "Raynham", add; label define city 246 "Reading", add; label define city 247
 "Rehoboth", add; label define city 248 "Revere", add; label define city 249 "Richmond", add; label define city 250 "Rochester",
 add; label define city 251 "Rockland", add; label define city 252 "Rockport", add; label define city 253 "Rowe", add; label
 define city 254 "Rowley", add; label define city 255 "Royalston", add; label define city 256 "Russell", add; label define city
 257 "Rutland", add; label define city 258 "Salem", add; label define city 259 "Salisbury", add; label define city 260
 "Sandisfield", add; label define city 261 "Sandwich", add; label define city 262 "Saugus", add; label define city 263 "Savoy",
 add; label define city 264 "Scituate", add; label define city 265 "Seekonk", add; label define city 266 "Sharon", add; label
 define city 267 "Sheffield", add; label define city 268 "Shelburne", add; label define city 269 "Sherborn", add; label define
 city 270 "Shirley", add; label define city 271 "Shrewsbury", add; label define city 272 "Shutesbury", add; label define city 273
 "Somerset", add; label define city 274 "Somerville", add; label define city 275 "Southampton", add; label define city 276
 "Southborough", add; label define city 277 "Southbridge", add; label define city 278 "South Hadley", add; label define city 279
 "Southwick", add; label define city 280 "Spencer", add; label define city 281 "Springfield", add; label define city 282
 "Sterling", add; label define city 283 "Stockbridge", add; label define city 284 "Stoneham", add; label define city 285
 "Stoughton", add; label define city 286 "Stow", add; label define city 287 "Sturbridge", add; label define city 288 "Sudbury",
 add; label define city 289 "Sunderland", add; label define city 290 "Sutton", add; label define city 291 "Swampscott", add;
 label define city 292 "Swansea", add; label define city 293 "Taunton", add; label define city 294 "Templeton", add; label define
 city 295 "Tewksbury", add; label define city 296 "Tisbury", add; label define city 297 "Tolland", add; label define city 298
 "Topsfield", add; label define city 299 "Townsend", add; label define city 300 "Truro", add; label define city 301
 "Tyngsborough", add; label define city 302 "Tyringham", add; label define city 303 "Upton", add; label define city 304
 "Uxbridge", add; label define city 305 "Wakefield", add; label define city 306 "Wales", add; label define city 307 "Walpole",
 add; label define city 308 "Waltham", add; label define city 309 "Ware", add; label define city 310 "Wareham", add; label define
 city 311 "Warren", add; label define city 312 "Warwick", add; label define city 313 "Washington", add; label define city 314
 "Watertown", add; label define city 315 "Wayland", add; label define city 316 "Webster", add; label define city 317 "Wellesley",
 add; label define city 318 "Wellfleet", add; label define city 319 "Wendell", add; label define city 320 "Wenham", add; label
 define city 321 "Westborough", add; label define city 322 "West Boylston", add; label define city 323 "West Bridgewater", add;
 label define city 324 "West Brookfield", add; label define city 325 "Westfield", add; label define city 326 "Westford", add;
 label define city 327 "Westhampton", add; label define city 328 "Westminster", add; label define city 329 "West Newbury", add;
 label define city 330 "Weston", add; label define city 331 "Westport", add; label define city 332 "West Springfield", add; label
 define city 333 "West Stockbridge", add; label define city 334 "West Tisbury", add; label define city 335 "Westwood", add; label
 define city 336 "Weymouth", add; label define city 337 "Whately", add; label define city 338 "Whitman", add; label define city
 339 "Wilbraham", add; label define city 340 "Williamsburg", add; label define city 341 "Williamstown", add; label define city
 342 "Wilmington", add; label define city 343 "Winchendon", add; label define city 344 "Winchester", add; label define city 345
 "Windsor", add; label define city 346 "Winthrop", add; label define city 347 "Woburn", add; label define city 348 "Worcester",
 add; label define city 349 "Worthington", add; label define city 350 "Wrentham", add; label define city 351 "Yarmouth", add;
 label define city 352 "Devens", add;
 label values town_res city;

replace gender=0 if gender==2;
label def gender 1 "Male" 0 "Female";
g byte female=(gender==0);
label values gender gender;
label def grade -1 "Pre-K" 0 "Kindergarten" 1 "1st" 2 "2nd" 3 "3rd" 4 "4th" 5 "5th" 6 "6th" 7 "7th"
	8 "8th" 9 "9th" 10 "10th" 11 "11th" 12 "12th" 13 "Post-Sec";
label values grade grade;
sort sasid masscode;
ren town_res town_res_perm;
tostring masscode, gen(masscode2);
g  strlen=strlen(masscode2) ;
g blah1=substr(masscode2, 1, 1) if strlen==5;
g blah2=substr(masscode2,1,2) if strlen==6;
g blah3=substr(masscode2,1,3) if strlen==7;
g town_res=blah1 if strlen==5;
replace town_res=blah2 if strlen==6;
replace town_res=blah3 if strlen==7;
drop blah* strlen masscode2;
destring town_res, replace;

*Manually define urban status;
qui egen urban=anymatch(town_res), values(35 44 49 57 61 93 95 97 100 128 137 149 153 160 163 165 201
236 243 248 274 281 293 348);
cap drop _merge;
drop town_res;
ren town_res_perm town_res;	
format masscode %12.0f;
save "$saves\sims_voter.dta", replace;

# delimit cr

*BASELINE VARIABLES
use "$saves/sims_voter.dta", clear 

*get baseline variables from year of application or prior
*or grade before/after to account for repeats/skips
keep if (year<=yearapp&grade<=gradeapp)
	drop if attend==0|attend==.|attend==555
				*Keep most attended for baseline school
				bys sasid year : egen attendmax = max(attend)
				keep if attend==attendmax
				drop attendmax
				duplicates drop 
				*only one duplicate remains (attendance tier)
				sample 1, count by(sasid year  attend )
				*keep closest to year of application
				bys sasid: egen max=max(year)
				keep if year==max
				drop max

	keep sasid year grade masscode yearapp gradeapp proj_year12 attend dob race low_inc town_res ///
		sped female lep

				*****demos*****
				* YEAR OF BIRTH
					qui g yob= year(dob) if dob!=.

				* Include Native Americans in other race category (numbers low in MA pop'n)
				* RACE
					qui g byte hisp= race==99 if race!=. 
					qui g byte black= race==3 if race!=. 
					qui g byte white= race==5 if race!=. 
					qui g byte asian= race==2 if race!=. 
					qui g byte otherrace= race==1 | race==4 if race!=. 

				* FREE REDUCED LUNCH
					qui g byte frpl= low_inc>=1 if low_inc~=.

				* SPECIAL ED
					qui g byte speced= sped>=1 if sped~=.
					drop sped
					ren speced sped

				drop race  low_inc
				
			qui egen bcharter=anymatch(masscode), ///
				values(4810550 4390050 4280305 4440205 4120530 ///
				4490305 4160305 4280305 4100205 4840505 4190305 ///
				4900305 4370505 4380505 4690505 4520505 4220505 ///
				4420050 4750505 4110305 4800405 4620305 4430205 ///
				4570205 4630205 4070405 4110305 4590305 4170205 ///
				4650105)  
				
				g hasbaselinedemos=1 if yob~=.&hisp~=.& black~=.& white~=.& asian~=.& otherrace~=.&female~=.&frpl~=.& sped ~=.&lep~=.&year~=.

				g inbostonbaseline=1 if bcharter==1|(masscode>=350000 & masscode<=359999)
				
	foreach v of varlist year grade masscode yob attend dob hisp black white asian otherrace frpl ///
		town_res bcharter sped female lep {
			ren `v' baseline`v'
			}
			
		compress
				save "$data/baselinedemos_voter.dta", replace
				
*CHARTER YEARS
use "$saves/sims_voter.dta", clear 
keep sasid yearapp year grade gradeapp attend masscode proj_year12 /*susp*/
duplicates drop
sort sasid year grade
	*count years after application
keep if grade>=gradeapp


				*bcharter (includes charters in Boston city limits))
					/*
					Excel Academy Charter 			4100205 
					Academy Of the Pacific Rim 		4120530 
					Boston Preparatory Charter 		4160305 
					Smith Leadership Academy Charter4190305 
					Roxbury Charter High  			4220505 
					Edward Brooke Charter 			4280305 
					City On A Hill Charter			4370505 
					City on a Hill Charter II	   35040505 open 2014
					Codman Academy Charter 			4380505 
					Conservatory Lab Charter 		4390050 open 2014
					Frederick Douglass Charter 		4420050 
					Neighborhood House Charter 		4440205 
					Boston Collegiate Charter 		4490305 
					Health Careers Academy HMCS		4520505 
					Dudley Street Neighbor          4070405 (opened in Sep.2012)
					Bridge Boston Dorchester		4170205
					Match Community Day charter     4650105 (opened in Sep.2011)
					MATCH (MATCH MS & HS)       	4690505 
					Boston Renaissance Charter 		4810550 
					Roxbury Preparatory Charter		4840505 
					*(This above becomes the mass code for all UCS: RoxPrep, Dorchester Prep, and Grove Hall in Sp 2012 lottery)
					Uphams Corner Charter (District)4900305 
					Dorchester collegiate academy 	4750505
					UP Academy of Boston 			4800405
					KIPP Academy Boston 			4630205 
					Brooke Charter School 2 Mattapan4430205
					Ed Brooke 3 East Boston  		4570205
					Boston Green Academy (BGA) 		4110305
					Excel 3 Boston Orient Heights	4590305
				
			*Additional charters outside of Boston
				Four Rivers Charter   				   	   4130505
				Berkshire Arts and Technology Charter 	   4140305
				Academy of Strategic Learning HMCS 		   4150505
				Christa McAuliffe Regional Charter 		   4180305
				Benjamin Banneker Charter 			  	   4200205
				Barnstable HMCS 				   		   4230010
				Marstons Mills East HM Charter 			   4270010
				Kipp Academy Lynn Charter 			   	   4290010
				Advanced Math and Science Academy Charter  4300305
				Cape Cod Lighthouse Charter 			   4320530
				Champion Charter  				  	       4340505
				Murdoch Middle Public Charter 			   4350305
				Community Charter School of Cambridge  	   4360305
				Community Day Charter Public Prospect      4400205
				Community Day Charter Gateway              4260205 (opened Sep 2012)
				Community Day Charter Kingman Webster	   4310205 (opened Sep 2012)
				Sabis International Charter 			   4410505
				Abby Kelley Foster Regional 			   4450105
				Foxborough Regional Charter    			   4460550
				Benjamin Franklin Classical Charter 	   4470205
				Hilltown Cooperative Charter Public  	   4500105
				Robert M. Hughes Academy Charter  		   4510550
				Holyoke Community Charter  			   	   4530005
				Lawrence Family Development Charter 	   4540205
				Hill View Montessori Charter Public  	   4550050
				Lowell Community Charter Public 		   4560050
				Lynn Community Charter				   	   4600105
				Marblehead Community Charter 			   4640305
				Martha's Vineyard Charter 			       4660550
				MAGNET: Ma Academy for Math and Science    4680505
				Mystic Valley Regional Charter 			   4700105
				New Leadership HMCS 				       4710405
				New Bedford Global HMCS  			       4720515
				North Central Charter Essential School     4740505
				Silver Hill Horace Mann Charter 		   4770010
				Francis W. Parker Charter Essential 	   4780505
				Pioneer Valley Performing Arts Charter     4790505 
				River Valley Charter 				       4820050
				Rising Tide Charter Public 			       4830305
				Salem Academy Charter 				       4850485
				Seven Hills Charter 				       4860105
				Prospect Hill Academy Charter   		   4870550
				South Shore Charter 				       4880550
				Sturgis Charter Public 				       4890505
				Atlantis Charter  				           4910550
				Martin Luther King Jr. Charter School 	   4920005
				Phoenix Charter Academy 			       4930505
				Pioneer Charter School of Science   	   4940205
				Global Learning Charter 			       4960305
				Pioneer Valley Chinese Immersion Charter   4970205
				Innovation Academy 					       4350305 (Murdoch Middle charter previously)
				Hampden charter school of science          4990305
				Boston Green academy                       4110305
				UP academy                                 4800405
				Grove hall preparatory                     4620305 (use Uncommon 4840505 in 2013 - )
				Spirit of knowledge charter school 		   4760505
				Salem Community Charter school             4670505
				Gloucester Community arts                  4480205 (opened in Sep.2010)
				Uncommon Schools                           4840505 (opened in Sep.2012)
				Dorchester collegiate 					   4750505
				Match Community Day charter                4650105 (opened in Sep.2011)
				Excel Chelsea 							   4610405 (opened in Sep.2011) 
  				*/

			g charter = ( masscode >=4000000&masscode<=4999999) | masscode== 35040505
			*spedalt charters
			replace charter = 0 if  masscode==4240505|masscode==4150505|masscode==4580505
			
			*excludes spedalt charters
			qui egen bcharter=anymatch(masscode), ///
				values(4810550 4390050 4280305 4440205 4120530 ///
				4490305 4160305 4280305 4100205 4840505 4190305 ///
				4900305 4370505 4380505 4690505 4520505 4220505 ///
				4420050 4750505 4110305 4800405 4620305 4430205 ///
				4570205 4630205 4070405 4110305 4590305 4170205 ///
				4650105 35040505) 
				
					g charter_notbos=charter-bcharter
		
*******lotteried charters*******
			*exclude charters we don't have lottery information for or who are disqualified
			*DEFINE STUDY SAMPLE HERE -- right now all lotteried charters
			*APR 4120530
			*BosCol 4490305
			*Boston Green Academy 4110305
			*Boston Prep 4160305
			*CoaH  4370505
			*CoaH II 35040505
			*Codman 4380505
			*EdBrooke (not 2 and 3) 4280305
			*Excel (not 3) 4100205
			*Match HS 4690505
			*Match MS 4690505
			*RoxPrep (not Uncommon or Grove Hall or DP) 4840505
			g blottocharter =  inlist(masscode, 4840505, 4690505,4100205, ///
				4280305, 4380505, 4370505, 4160305, 4110305 , 4490305 ,35040505,  4120530)
			
			*LTO sample: APR BosCol BosPrep CoaH Codman Match HS
			g bltocharter =  inlist(masscode, 4690505, ///
				4380505, 4370505, 4160305 , 4490305 , 4120530)
			
			g urbancharter=blottocharter
			*global and kipp lynn
				*	Salem Academy Charter 				       4850485
			replace urbancharter=1 if inlist(4960305,4290010, 4850485)
			
			*	Four Rivers Charter   				   	   4130505
			*	Cape Cod Lighthouse Charter 			   4320530
			*	Marblehead Community Charter 			   4640305
			*	Francis W. Parker Charter Essential 	   4780505
			*	Pioneer Valley Performing Arts Charter     4790505
			*	Rising Tide Charter Public 			       4830305
			*	Sturgis Charter Public 				       4890505
			* Innovation Academy 					       4350305 (Murdoch Middle charter previously)

			g nonurbancharter=inlist(masscode, 4350305 , 4890505 , 4850485 , ///
				4830305, 4780505 , 4640305 , 4320530 , 4130505, 4790505)
				
			
				
******* end lotteried charters*******
				
			*exam
			*MA Academy of Math and Science 4680505 is a public magnet school
			qui egen  exam=anymatch(masscode), values(350560 350545 350575 4680505)
			 
			*spedalt;
			*Let's move the following charters to alternative schools:
			*Boston Day and Evening 4240505
			*Egelston  
			*Amesbury Charter       4150505 
			*Lowell Middlesex 		4580505
			* two virtual schools    39010900 39020900
			qui egen  spedalt=anymatch(masscode), ///
				values(4150505 4580505 350543 1670815 9500203 1510805 ///
				260805 9500245 2070802 9500442 460850 460875 1130803 ///
				300805 9500430 2440810 9500403 1980805 9500404 3320805 ///
				1130812 260870 1220805 400807 1570805 490811 500820 ///
				2100810 2070801 1550806 350725 9500417 1440815 ///
				2880810 3430820 1550810 2880815 9500454 1020805 ///
				2810850 2100835 100815 1680805 9500418 2570805 ///
				1470810 1130810 500815 9500458 9500450 9500443 1850805 ///
				1370840 1820805 950820 490815 9500407 3480820 ///
				100820 3300840 1700820 400820 9500456 9500412 2100823 ///
				9500463 1800810 70830 1520810 1520805 350738 9500428 ///
				1210850 350749 460811 500825 3480960 2810845 350758 /// 
				2010820 9500402 2050810 3190805 9500415 300920 ///
				9500510 410810  3070840 1000845 2070867 9500270 560845 ///
				1530840 3070830 350768 400840 3320830 440860 ///
				550805 400812 1510810 1860820 2920813 3470830 ///
				3480865 830850 2810816 1370825 310805 3260830 ///
				2100818 2760820 9500462 3320815 1550815 960810 ///
				3140890  9500427 90850 3080815 1470830 2390815 1000860 ///
				9500455 1370835 1600920 2610805 3080850 2010845 350809 ///
				2700850 1150805 2540810 270880 200850 1820830 1810812 ///
				950940 9500460 210810 2920810  2920820 2390805 350791 ///
				2440840 9500268 560835 20830 860817 610845 1520840 670802 ///
				1990820 1700803 9500429 3250840 1020825 1590815 2880805 ///
				9500461 9500420 4240505 350543 350580 350588 350036 ///
				350363 350413 350548 350750 350518 351001 351001 1601003 ///
				7731001 1811001 3251004 3251004 1861001 6401002 6151001 ///
				3471001 2581001 7751001 1821001 6801001 1261002 201001 ///
				1011001 6001001 441001 301001 201003 2431004 351002 351002 ///
				4241001 4811001 401002 401001 441004 571002 491001 ///
				1371001 2071002 2071002 6951001 351016 571001 611001 ///
				351003 571003 1141001 351009 3481007 2091001 351018 ///
				351018 6401001 1361001 2091002 951004 951004 1761001 711001 ///
				6451001 351004 951003 351005 6051002 351006 1001002 2431003 ///
				6251001 6251001 961001 3481002 971001 8151002 7551003 ///
				1031002 441002 2441002 2441002 3481004 3481004 2431005 ///
				8151001 521001 351010 351010 1261001 1281001 2361001 ///
				1491002 4531001 1811002 7401001 3251003 7551001 2431002 ///
				441005 1511002 351007 1601002 1601002 4581001 ///
				6001002 1651001 1671002 1671001 1701001 1721001 1601001 ///
				1601001 351011 7551004 3361001 7551004 6831001 1971001 ///
				1981001 3481006 2431006 2431001 351017 351017 351008 ///
				6741001 2651001 651001 2141002 2141002 2011001 2011001 ///
				2291001 4931001 2391001 6351001 6601001 3311001 2261001 ///
				201002 441003 1031001 3481001 231001 2441001 1511001 ///
				951001 1001001 951002 7651001 6751001 3481005 1491001 ///
				2641001 2481001 6581001 571005 6051001 2781001 2071001 ///
				2811001 351014 721001 351013 3251001 1051001 1601004 ///
				2141001 3041001 7001001 501001 2121001 2121001 6221001 ///
				1851001 6701001 3101001 3101001 3321001 3361002 3361002 ///
				3361002 3361003 7801001 351015 3481003 3481003 101001 571004 ///
				351012 50010101 50030101 50640101 50060101 50040101 50930101 ///
				50490101 50050101 50050102 50070102 50070101 50130102 50080101 ///
				50080102 50100101 50100102 50420101 50420102 50640201 50130101 ///
				50130103 50140101 50200101 50200201 50200202 50200203 50210101 ///
				50250102 50250101 50240101 50240102 50410101 50410201 ///
				50110101 50980101 50260101 50260102 50270101 50450101 ///
				50020101 50850101 50850102 50290101 50290102 50290103 ///
				50300101 50300102 50310101 50330101 50330102 50820101 ///
				50340101 50350101 50360101 50360102 50090101 50380101 ///
				50390101 50390102 50490301 50610201 50610101 50710101 ///
				50400101 50400102 50460101 50460102 50590101 50480101 ///
				50190101 50370101 50500101 50510101 50530101 50530102 ///
				50540101 50550101 50550102 50580101 50570101 50230101 ///
				50440201 50440202 50470101 50470102 50620101 50620102 ///
				50620401 50620301 50620302 50630101 50980201 50490601 ///
				50650101 50650102 50160101 50120101 50190201 50680101 ///
				50700101 50670101 50280101 50690101 50690103 50690102 ///
				50690104 50690105 50170101 50490401 50750101 50760101 ///
				50760102 50760105 50760106 50760107 50760108 50760103 ///
				50760104 50770101 50780101 50780102 50820201 50790101 ///
				50800101 50730101 50810101 50890101 50180101 50850201 ///
				50860101 50410301 50220101 50220102 50220103 50830101 ///
				50830102 50840101 50870101 50880101 51000100 50490201 ///
				50440101 50440102 50440102 50440301 50440302 50560101 ///
				50560102 50560104 50560103 50490501 50720101 50720102 ///
				50910101 50910102 50560201 50930202 50930201 50940101 ///
				50940102 50600101 50950101 50960101 50970101 1670815 ///
				1510805 260805 2070802 460850 460875 1130803 300805 2440810 ///
				400807 1980805 3320805 1210850 260870 1220805 1570805 ///
				2100810 500820 2070801 1550806 350725 1440815 2880810 ///
				3430820 1550810 2880815 1020805 2810850 2100835 1410850 ///
				100815 1680805 2570805 1130810 1850805 1370840 1820805 ///
				950820 490815 3480820 100820 3300840 1700820 400840 2100823 ///
				70830 1520810 350749 460811 500825 3480960 2810845 350758 ///
				2010820 2050810 300920 410810 3070840 2070867 560845 ///
				1530840 3070830 350768 3320830 440860 1510810 1860820 ///
				2920813 3470830 3480865 830850 2810816 1370825 310805 ///
				3260830 2100818 2760820 3320815 1550815 960810 3140890 ///
				90850 3080815 1980830 1470830 2390815 1000860 1600920 ///
				2610805 3080850 2010845 350809 2700850 1150805 2540810 ///
				270880 200850 1820830 1810812 950940 210810 2920810 ///
				3480802 2920820 2390805 1520805 350791 1000845 2440840 ///
				20830 860817 610845 670802 1990820 1700803 3250840 ///
				1020825 1590815 2880805 300865 300870 350759  ///
				690805 3280820 1700805 790850 1660810 710860 1370845 ///
				750805 400850 1860825  39010900 39020900)
				qui replace spedalt=1 if masscode >= 50000000 & masscode <= 59999999

			*boston
			qui gen boston=0 if masscode~=.
			qui replace boston=1 if (masscode>=350000 & masscode<=359999)|bcharter==1
			
		*deal with duplicates/repeaters etc
		drop if attend==.|attend==0|attend==555
 			drop if grade>=13
			

		*number of schools that student has attended in year and grade
		egen sasidXyearXgrade = group(sasid year grade)
		quietly unique masscode, by(sasidXyearXgrade) generate(unique)
		qui egen numschools=sum(unique), by(sasidXyearXgrade)
		drop unique sasidXyearXgrade
		
		*assign school within a year
*Assign students to most attended school
		duplicates report sasid year grade
		
		
		*attendance data goes across schools
		bys sasid year grade : egen totattend = sum(attend)
		

		*KEEP OBS BASED ON SCHOOL ASSIGNMENT;
		*for students with no special schools, keep most attended
		*For students who attend charter, keep charter
		bys sasid year grade:  egen special=max(charter)

		bys sasid year grade: egen maxattend=max(attend)
		qui gen keepflag=1 if maxattend==attend
		qui replace keepflag=1 if special==1
		*keep most attended and keep all records of those who have attended special schools
		keep if keepflag==1
		drop max keepflag

		*keep charter school if other school is regular
		drop if charter==0 &(special==1)
		
		*now keep most attended special within a year
		bys sasid year :  egen  maxattend=max(attend)
		keep if attend==maxattend
		*this also drops people with two grades in one year and assigns to most attended

		*now only attendance ties left!
		*D.SUN 12/9/2013: seed already set on top of this do file.
		sample 1, count by(sasid year)

		drop max* special    
		order sasid year grade masscode totattend attend charter masscode 
		sort sasid year
		qui compress
		
		
		*flag repeaters
		g blah =1
		bys sasid grade: egen repeats=sum(blah)
		replace repeats=repeats-1
		label var repeats "number of additional years in grade"
		
		
		foreach c in charter blottocharter bltocharter bcharter charter_notbos urbancharter nonurbancharter{
		bys sasid grade `c': egen `c'_repeats=sum(blah) if `c'==1
		replace `c'_repeats=`c'_repeats-1
				replace `c'_repeats=0 if `c'_repeats==.
		label var `c'_repeats "number of additional years in grade in `c'"
		}
		
		
				drop blah

		* NOW keep first time in grade 
		bys sasid grade: egen minyear=min(year)
		keep if year==minyear
		drop minyear
		
		
		save "$data/sims_voter_long.dta", replace
	
		use "$data/sims_voter_long.dta", clear
		
		tostring grade, replace force
		replace grade = "pk3" if grade=="-1"
		replace grade = "pk4" if grade=="-.5"
		replace grade = "k" if grade =="0"
		reshape wide year masscode totattend attend bcharter  bltocharter blottocharter charter charter_notbos urbancharter nonurbancharter exam spedalt boston numschools *repeats ///
			, i(sasid ) j(grade) string

		save "$data/sims_voter_wide.dta", replace
